const db = require('../../lib/conn_oracle')

exports.getSubsysInfo  = async (req,res)=>{
    // console.log(req.body)
    let {params} = req.body
    let {specialties} = params
    // console.log(specialties)
    let resDatas = []
    let resMsg = ""
    let reqDatas = []
    let specialtiesWhere
    if(specialties && specialties.length > 1){
        specialtiesWhere = `and cust_specnum in (`
        for (let i = 0; i < specialties.length; i++)
        specialtiesWhere += (i > 0)? ", :" + i : ":" + i
        specialtiesWhere += ")"  
        reqDatas.push(...specialties)
    }else if(specialties && specialties.length === 1){
        specialtiesWhere = `and cust_specnum = :specialties`
        reqDatas.push(...specialties)
    }else{specialtiesWhere = `and cust_specnum in ('FAS','GQT','XSW')`}
    let sql = `select * from c##wxw.subsys
               where 1 = 1
               ${specialtiesWhere}
               order by cust_subsys
              `    
    try{
        let subsysInfo = await db.search(sql,reqDatas)
        resMsg = subsysInfo.errorNum ? "查询失败" : "查询成功"
        resStatus = subsysInfo.errorNum ? 0 : 1
        resDatas = { subsysInfo }
        res.json({
            resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg: "查询失败" + err,
            status: 0
        })
    }
}

exports.editSubsys = async (req,res)=>{
    // console.log(req.body)
    let {type,data} = req.body
    let {CUST_SUBSYS,SUBSYS,SUB_DESC,CUST_SPECNUM,UNIT} = data
    let resMsg = ""
    let msgType = ""
    let resStatus = 1
    let resDatas = {}
    let editDatas = []
    let editSql = ""
    if(type === "add" || type === "copy"){
        msgType = "新增"
        editDatas = [ CUST_SUBSYS,SUBSYS,SUB_DESC,CUST_SPECNUM,UNIT ]
        editSql = `insert into c##wxw.subsys(CUST_SUBSYS,SUBSYS,SUB_DESC,CUST_SPECNUM,UNIT)
                   values(:CUST_SUBSYS,:SUBSYS,:SUB_DESC,:CUST_SPECNUM,:UNIT)`
    }
    if(type === "update"){
        msgType = "更新"
        editDatas = [ SUBSYS,SUB_DESC,CUST_SPECNUM,UNIT,CUST_SUBSYS ]
        editSql = `update c##wxw.subsys 
                    set SUBSYS=:SUBSYS,
                    SUB_DESC=:SUB_DESC,
                    CUST_SPECNUM=:CUST_SPECNUM,
                    UNIT=:UNIT 
                    where CUST_SUBSYS=:CUST_SUBSYS`
    }
    if(type === "delete"){
        msgType = "删除"
        editDatas = [ CUST_SUBSYS ]
        editSql = `delete from c##wxw.subsys where CUST_SUBSYS=:CUST_SUBSYS`
    }

    try{
        let newSubsys = await db.writeTab(editSql,editDatas)
        resMsg = newSubsys.errorNum ? `${msgType}失败` : `${msgType}成功`
        resStatus = newSubsys.errorNum ? 0 : 1
        resDatas = { newSubsys }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg:`${msgType}失败`+err,
            status:0
        })
    }
    
}

exports.batchEditSubsys = async (req, res) => {
    // console.log(req.body)
    let { type, data } = req.body
    let resMsg = ""
    let msgType = ""
    let resStatus = 1
    let resDatas = {}
    let editDatas = []
    let editSql = ""
    let options = {
        autoCommit: true
    }
    if(type === "batchAdd"){
        msgType = "批量新增"
        editDatas = data
        editSql = `insert into c##wxw.subsys(CUST_SUBSYS,SUBSYS,SUB_DESC,CUST_SPECNUM,UNIT)
                   values(:CUST_SUBSYS,:SUBSYS,:SUB_DESC,:CUST_SPECNUM,:UNIT)`
    }
    if(type === "batchUpdate"){
        msgType = "批量更新"
        editDatas = data
        editSql = `update c##wxw.subsys 
                    set SUBSYS=:SUBSYS,
                    SUB_DESC=:SUB_DESC,
                    CUST_SPECNUM=:CUST_SPECNUM,
                    UNIT=:UNIT 
                    where CUST_SUBSYS=:CUST_SUBSYS`
    }
    if(type === "batchDel"){
        msgType = "批量删除"
        editDatas = data
        editSql = `delete from c##wxw.subsys where CUST_SUBSYS=:CUST_SUBSYS`
    }

    try{
        let newSubsys = await db.writeManyTab(editSql,editDatas,options)
        resMsg = newSubsys.errorNum ? `${msgType}失败` : `${msgType}成功`
        resStatus = newSubsys.errorNum ? 0 : 1
        resDatas = { newSubsys }
        res.json({
            msg:resMsg,
            status: resStatus,
            resDatas
        })
    }catch(err){
        res.json({
            msg:`${msgType}失败`+err,
            status:0
        })
    }

}